Ext.onReady(function() {
    Ext.QuickTips.init();
    //获取车辆列表
    var carList_store = new Ext.data.Store({
        storeId: 'store3',
        proxy: new Ext.data.HttpProxy({
            url: 'Get_Car_List.action'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                }, {
                    name: 'value'
                }
            ]
        })
    });

    carList_store.load();

    // 元数据的store
    var vehicleusestore = new Ext.data.Store({
        storeId: 'store',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=vehicleuse'
        }),
        reader: new Ext.data.JsonReader({
            fields: [{
                    name: 'name'
                }, {
                    name: 'value'
                }]
        })
    });

    var storecompany = new Ext.data.Store({
        storeId: 'storecompany',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=company'
        }),
        reader: new Ext.data.JsonReader({
            fields: [{
                    name: 'name'
                }, {
                    name: 'value'
                }]
        })
    });
    storecompany.load();

    var storedepartment = new Ext.data.Store({
        storeId: 'storedepartment',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=department'
        }),
        reader: new Ext.data.JsonReader({
            fields: [{
                    name: 'name'
                }, {
                    name: 'value'
                }]
        })
    });
    
    storedepartment.load();

    var storetype = new Ext.data.Store({
        storeId: 'storetype',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=type'
        }),
        reader: new Ext.data.JsonReader({
            fields: [{
                    name: 'name'
                }, {
                    name: 'value'
                }]
        })
    });
    
    storetype.load();


    var storeattributes = new Ext.data.Store({
        storeId: 'storeattributes',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=attributes'
        }),
        reader: new Ext.data.JsonReader({
            fields: [{
                    name: 'name'
                }, {
                    name: 'value'
                }]
        })
    });
    
    storeattributes.load();

    var storelogo = new Ext.data.Store({
        storeId: 'storelogo',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=logo'
        }),
        reader: new Ext.data.JsonReader({
            fields: [{
                    name: 'name'
                }, {
                    name: 'value'
                }]
        })
    });

storelogo.load();


   var BasicInformation = new Ext.FormPanel({
        id: 'BasicInformation',
        name: 'BasicInformation',
        labelWidth: 80,
        width: '100%',
        height: '100%',
        bodyStyle: 'padding: 4px',
        border: false,
        autoscroll: true,
        frame: true,
        autoScroll: true,
        items: [{
                layout: 'column',
                items: [{
                        columnWidth: .5,
                        layout: 'form',
                        items: [{
                                xtype: 'hidden',
                                name: 'id'
                            }, {
                                xtype: 'textfield',
                                fieldLabel: '车牌号码',
                                emptyText: '车牌号码(必填)',
                                allowBlank: false,
                                name: 'code',
                                anchor: '95%'
                            }, new Ext.form.ComboBox({
                                fieldLabel: '资产属性',
                                hiddenName: 'attributes',
                                store: storeattributes,
                                displayField: 'name',
                                valueField: 'name',
                                typeAhead: true,
                                triggerAction: 'all',
                                emptyText: '---请选择---',
                                allowBlank: false,
                                anchor: "95%",
                                name: 'attributes',
                                mode : 'local', 
                                selectOnFocus: true
                            })]
                    }, {
                        columnWidth: .5,
                        layout: 'form',
                        items: [
                            new Ext.form.ComboBox({
                                fieldLabel: '产权单位名称',
                                hiddenName: 'company',
                                store: storecompany,
                                displayField: 'name',
                                valueField: 'name',
                                typeAhead: true,
                                triggerAction: 'all',
                                emptyText: '---请选择---',
                                allowBlank: false,
                                anchor: "95%",
                                name: 'company',
                                mode : 'local',
                                selectOnFocus: true
                            }),
                            new Ext.form.ComboBox({
                                fieldLabel: '使用部门名称',
                                hiddenName: 'department',
                                store: storedepartment,
                                displayField: 'name',
                                valueField: 'name',
                                typeAhead: true,
                                triggerAction: 'all',
                                emptyText: '---请选择---',
                                allowBlank: false,
                                anchor: "95%",
                                name: 'department',
                                mode : 'local',
                                selectOnFocus: true
                            })]
                    }, {
                        columnWidth: .5,
                        layout: 'form',
                        items: [new Ext.form.ComboBox({
                                fieldLabel: '车辆类型',
                                hiddenName: 'type',
                                store: storetype,
                                displayField: 'name',
                                valueField: 'name',
                                typeAhead: true,
                                triggerAction: 'all',
                                emptyText: '---请选择---',
                                allowBlank: false,
                                anchor: "95%",
                                mode : 'local',
                                name: 'type',
                                selectOnFocus: true
                            }), {
                                xtype: 'textfield',
                                fieldLabel: '车辆原值',
                                emptyText: '车辆原值(万)(必填)',
                                allowBlank: false,
                                name: 'price',
                                anchor: '95%'
                            }]
                    }, {
                        columnWidth: .5,
                        layout: 'form',
                        items: [{
                                xtype: 'textfield',
                                fieldLabel: '制作厂商',
                                emptyText: '制作厂商(必填)',
                                allowBlank: false,
                                name: 'manufacturer',
                                anchor: '95%'
                            }, {
                                xtype: 'textfield',
                                fieldLabel: '驾驶员姓名',
                                name: 'driver',
                                anchor: '95%'
                            }]
                    }, {
                        columnWidth: .5,
                        layout: 'form',
                        items: [{
                                xtype: 'textfield',
                                fieldLabel: '品牌',
                                emptyText: '品牌(必填)',
                                allowBlank: false,
                                name: 'brand',
                                anchor: '95%'
                            }, {
                                xtype: 'textfield',
                                fieldLabel: '排量',
                                emptyText: '排量(必填)',
                                allowBlank: false,
                                name: 'displacement',
                                anchor: '95%'
                            }]
                    }, {
                        columnWidth: .5,
                        layout: 'form',
                        items: [{
                                xtype: "radiogroup",
                                fieldLabel: "使用公司标识",
                                disabled: false,
                                id: 'logo',
                                name: 'logo',
                                anchor: "95%",
                                items: [{
                                        boxLabel: '是',
                                        name: 'logo',
                                        inputValue: '1'
                                    }, {
                                        boxLabel: '否',
                                        name: 'logo',
                                        inputValue: '2',
                                        checked: true
                                    }]
                            }, {
                                xtype: "radiogroup",
                                fieldLabel: "是否安装GPS",
                                disabled: false,
                                id: 'gps',
                                name: 'gps',
                                anchor: "95%",
                                items: [{
                                        boxLabel: '是',
                                        name: 'gps',
                                        inputValue: '1'
                                    }, {
                                        boxLabel: '否',
                                        name: 'gps',
                                        inputValue: '2',
                                        checked: true
                                    }]
                            }]
                    }, {
                        columnWidth: .5,
                        layout: 'form',
                        items: [{
                                xtype: 'datefield',
                                fieldLabel: '购车日期',
                                name: 'purchase_date',
                                emptyText: '购车日期(必填)',
                                allowBlank: false,
                                altFormats: 'Ymd',
                                format: 'Y-m-d',
                                dateFormat: 'Ymd',
                                anchor: '95%'
                            }, {
                                xtype: 'datefield',
                                fieldLabel: '使用日期',
                                name: 'use_date',
                                altFormats: 'Ymd',
                                format: 'Y-m-d',
                                dateFormat: 'Ymd',
                                anchor: '95%'
                            }]

                    }]

            }]
    });

    var BasicInformation_add_win = new Ext.Window({
        id: 'BasicInformation_win',
        title: "添加车辆基本信息",
        border: false,
        bodyBorder: false,
        // layout:'fit',
        width: 630,
        closeAction: 'hide',
        modal: true,
        tbar: ['-', {
                text: ' 提交并继续',
                handler: function() {
                    onSubmit();
                }
            }, '-', {
                text: ' 提交并返回',
                handler: function() {
                    onSubmit();
                    BasicInformation_add_win.hide();
                }
            }, '-', {
                text: ' 返回',
                handler: function() {
                    //BasicInformation.getForm().reset();
                    BasicInformation_add_win.hide();
                }
            }, '-', {
                text: ' 重置',
                handler: function() {
                    Ext.getCmp("BasicInformation").getForm().reset();
                }
            }, '-'],
        items: [BasicInformation]
    });

    function onSubmit() {
        // 提交表单
        if (BasicInformation.getForm().isValid()) {
            var submitValues = BasicInformation.getForm().getValues();
            for (var param in submitValues) {
                if (BasicInformation.form.findField(param)
                        && BasicInformation.form.findField(param).emptyText == submitValues[param]) {
                    BasicInformation.form.findField(param).setValue(' ');
                }
            }
            BasicInformation.getForm().submit({
                url: 'BasicInformationAction.action',
                waitMsg: '数据保存中...',
                submitEmptyText: false,
                success: function(c, d) {
                    Ext.MessageBox.alert("操作信息", "成功信息保存！");
                    store.reload();
                    BasicInformation.getForm().reset();
                },
                failure: function(c, d) {
                    Ext.MessageBox.show({
                        title: "操作信息",
                        msg: "信息保存出错，请联系管理员！",
                        buttons: Ext.MessageBox.OK,
                        icon: "ext-mb-error"
                    });

                }
            });

        }

    }



    var toolbar = new Ext.Toolbar({
        id: 'toolbar',
        stateful: true,
        items: ['-', {
                id: 'add-p',
                text: '新增',
                handler: function() {
                    BasicInformation_add_win.show();
                }
            }, '-', {
                id: 'edit-btn',
                text: '编辑',
                handler: function() {
                    edit();
                }
            }, '-', {
                id: 'del-btn',
                text: '删除',
                handler: function() {
                    Ext.MessageBox.show({
                        title: '提示框',
                        msg: ' 确定删除该条信息吗？注意，数据删除后将无法恢复！ ',
                        buttons: {
                            'yes': '确定',
                            'no': '取消'
                        },
                        fn: showResult,
                        icon: Ext.MessageBox.QUESTION
                    });

                    function showResult(btn) {
                        if (btn == 'yes') {
                            del();
                        }
                    }

                }
            }, '-', {
                text: '导出Excel',
                handler: function() {
                    window.open("ExportXls_BasicInformation.action");
//                    Ext.Ajax.request( {
//                        url : 'ExportXls_BasicInformation.action',
//                        params : {
//                            code:Ext.ComponentMgr.get("code_from").getValue(),
//                            company:Ext.ComponentMgr.get("company_from").getValue(),
//                            department:Ext.ComponentMgr.get("department_from").getValue(),
//                            type:Ext.ComponentMgr.get("type_from").getValue(),
//                            attributes:Ext.ComponentMgr.get("attributes_from").getValue(),
//                            logo:Ext.util.Format.date(Ext.ComponentMgr.get("logo_from").getValue(), 'Ymd')
//                        },
//                        success : function(response, options) {
//
//                        },
//                        failure : function(response, options) {
//
//                        }
//                    });
                }
            }, '-']
    });


    function edit() {
        var edit_data = Ext.ComponentMgr.get("ggg")
                .getSelectionModel().getSelections();
        if (edit_data.length == 1) {
            BasicInformation.getForm().loadRecord(edit_data[0]);
            BasicInformation_add_win.show();
        } else {
            Ext.MessageBox.alert("提示", "只能编辑一行数据");
        }

    }

    function del() {
        var del_data = Ext.ComponentMgr.get("ggg")
                .getSelectionModel().getSelections();
        if (del_data.length == 0) {
            Ext.Msg.alert('提示', '请选择需要删除的数据！');
            return;
        } else {
            var id_array = [];
            for (var i = 0; i < del_data.length; i++) {
                id_array[i] = del_data[i].data.id;
            }
            sendRequest(id_array);
        }
    }
    function sendRequest(data) {
        Ext.Ajax.request({
            url: 'DeleteAction.action',
            params: {
                id: data,
                type: 'car'
            },
            success: function(response, options) {
                Ext.Msg.alert('提示', '删除成功！');
                store.reload();
            },
            failure: function(response, options) {
                Ext.Msg.alert('提示', '删除失败！');
            }
        });
    }

    var form = new Ext.FormPanel({
        labelAlign: 'form',
        frame: true,
        bodyStyle: 'padding:5px 5px 0',
        width: '100%',
        items: [{
                layout: 'column',
                items: [{
                        columnWidth: .3,
                        layout: 'form',
                        items: [{
                                xtype: 'combo',
                                fieldLabel: '车牌号码',
                                name: 'code_from',
                                id: 'code_from',
                                displayField: 'name',
                                valueField: 'name',
                                emptyText: '车牌号码',
                                anchor: '95%',
                                typeAhead: true,
                                queryParam: 'name',
                                autoSelect: false,
                                triggerAction: 'all',
                                store: carList_store
                            }, new Ext.form.ComboBox({
                                fieldLabel: '使用公司标识',
                                hiddenName: 'logo',
                                store: storelogo,
                                displayField: 'name',
                                valueField: 'value',
                                typeAhead: true,
                                triggerAction: 'all',
                                emptyText: '---请选择---',
                                anchor: "95%",
                                id: 'logo_from',
                                selectOnFocus: true
                            })]
                    }, {columnWidth: .3,
                        layout: 'form',
                        items: [
                            new Ext.form.ComboBox({
                                fieldLabel: '产权单位名称',
                                hiddenName: 'company',
                                store: storecompany,
                                displayField: 'name',
                                valueField: 'name',
                                typeAhead: true,
                                triggerAction: 'all',
                                emptyText: '---请选择---',
                                anchor: "95%",
                                id: 'company_from',
                                selectOnFocus: true
                            }),
                            new Ext.form.ComboBox({
                                fieldLabel: '使用部门名称',
                                hiddenName: 'department',
                                store: storedepartment,
                                displayField: 'name',
                                valueField: 'name',
                                typeAhead: true,
                                triggerAction: 'all',
                                emptyText: '---请选择---',
                                anchor: "95%",
                                id: 'department_from',
                                selectOnFocus: true
                            })]}, {columnWidth: .3,
                        layout: 'form',
                        items: [
                            new Ext.form.ComboBox({
                                fieldLabel: '车辆类型',
                                hiddenName: 'type',
                                store: storetype,
                                displayField: 'name',
                                valueField: 'name',
                                typeAhead: true,
                                triggerAction: 'all',
                                emptyText: '---请选择---',
                                anchor: "95%",
                                id: 'type_from',
                                selectOnFocus: true
                            }),
                            new Ext.form.ComboBox({
                                fieldLabel: '资产属性',
                                hiddenName: 'attributes',
                                store: storeattributes,
                                displayField: 'name',
                                valueField: 'name',
                                typeAhead: true,
                                triggerAction: 'all',
                                emptyText: '---请选择---',
                                anchor: "95%",
                                id: 'attributes_from',
                                selectOnFocus: true
                            })]}, {columnWidth: .1,
                        layout: 'form',
                        items: [
                        ]}, {
                        columnWidth: .1,
                        layout: 'form',
                        items: [{
                                name: 'iPage',
                                id: 'iPage',
                                xtype: 'hidden'
                            }, {
                                xtype: "button",
                                id: 'query_tbar',
                                text: "查  询",
                                width: 60,
                                handler: function() {
                                    store.setBaseParam("code", Ext.ComponentMgr.get("code_from").getValue());
                                    store.setBaseParam("company", Ext.ComponentMgr.get("company_from").getValue());
                                    store.setBaseParam("department", Ext.ComponentMgr.get("department_from").getValue());
                                    store.setBaseParam("type", Ext.ComponentMgr.get("type_from").getValue());
                                    store.setBaseParam("attributes", Ext.ComponentMgr.get("attributes_from").getValue());
                                    store.setBaseParam("logo", Ext.ComponentMgr.get("logo_from").getValue());
                                    store.load();
                                }
                            }]
                    }]

            }






        ]
    })

    jReader1 = new Ext.data.JsonReader({
        root: 'data',
        totalProperty: 'totalCount',
        fields: [{
                name: 'id'
            }, {
                name: 'code'
            }, {
                name: 'attributes'
            }, {
                name: 'type'
            }, {
                name: 'price'
            }, {
                name: 'brand'
            }, {
                name: 'displacement'
            }, {
                name: 'purchase_date'
            }, {
                name: 'use_date'
            }, {
                name: 'company'
            }, {
                name: 'department'
            }, {
                name: 'manufacturer'
            }, {
                name: 'driver'
            }, {
                name: 'logo'
            }, {
                name: 'gps'
            }, {
                name: 'vehicleuse'
            }]
    });

    var store = new Ext.data.Store({
        storeId: 'store1',
        proxy: new Ext.data.HttpProxy({
            url: 'BasicInformationJson.action'
        }),
        reader: jReader1,
        baseParams: {
        }
    });

    var sm = new Ext.grid.CheckboxSelectionModel({
    });

    var cm = new Ext.grid.ColumnModel([sm, {
            header: '编号',
            sortable: true,
            hidden: true,
            dataIndex: 'id'
        }, {
            header: '车牌号码',
            sortable: true,
            hidden: false,
            width: 76,
            dataIndex: 'code'
        }, {
            header: '资产属性',
            sortable: true,
            width: 83,
            dataIndex: 'attributes'
        }, {
            header: '车辆类型',
            sortable: true,
            width: 151,
            dataIndex: 'type'
        }, {
            header: '车辆原值(万)',
            sortable: true,
            width: 76,
            dataIndex: 'price'
        }, {
            header: '品牌',
            sortable: true,
            width: 83,
            dataIndex: 'brand'
        }, {
            header: '排量',
            sortable: true,
            width: 45,
            dataIndex: 'displacement'
        }, {
            header: '购买日期',
            sortable: true,
            width: 81,
            dataIndex: 'purchase_date'
        }, {
            header: '使用日期',
            sortable: true,
            width: 84,
            dataIndex: 'use_date'
        }, {
            header: '产权单位',
            sortable: true,
            width: 150,
            dataIndex: 'company'
        }, {
            header: '使用部门',
            sortable: true,
            width: 98,
            dataIndex: 'department'
        }, {
            header: '制造商',
            sortable: true,
            width: 176,
            dataIndex: 'manufacturer'
        }, {
            header: '驾驶员姓名',
            sortable: true,
            width: 81,
            dataIndex: 'driver'
        }, {
            header: '是否使用公司标识',
            sortable: true,
            width: 109,
            dataIndex: 'logo',
            renderer: function(v) {
                if (v == "1") {
                    return "是";
                } else if (v == "2") {
                    return "否";
                }
            }
        }, {
            header: '是否安装GPS',
            sortable: true,
            width: 100,
            dataIndex: 'gps',
            renderer: function(v) {
                if (v == "1") {
                    return "是";
                } else if (v == "2") {
                    return "否";
                }
            }
        }, {
            header: '车辆用途',
            hidden: true,
            sortable: true,
            dataIndex: 'vehicleuse'
        }]);

    var grid = new Ext.grid.GridPanel({
        id: 'ggg',
        store: store,
        anchor: '100% -80',
        viewConfig: {
            forceFit: false
        },
        sm: sm,
        cm: cm,
        frame: true,
        columnLines: true,
        autoWidth: true,
        loadMask: true, // 数据加载时的遮罩功能
        stateful: true,
        stateId: 'grid'
//			pageSize: 20,
//			bbar : new Ext.PagingToolbar( {
//				store : store,
//				emptyMsg : '无数据',
//				displayInfo : true
//			})
    });
    store.load();

    var viewport = new Ext.Viewport({
        layout: 'border',
        items: [{
                region: 'center',
                layout: 'border',
                items: [{
                        tbar: toolbar,
                        region: 'center',
                        // layout : 'accordion',
                        layout: "anchor",
                        frame: true,
                        deferredRender: false,
                        activeTab: 0,
                        items: [form, grid]
                    }]
            }]
    });


})